<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="李宏毅ML2021-Spring-hw1：Covid19 Cases Prediction, python,machine learning,deep learning,html,css,c,c++,cpp,cmake,ros,linux,ubuntu">
    <meta name="description" content="本文主要讲解李宏毅ML2021 Spring的Homework1： Covid19 Cases Prediction">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>李宏毅ML2021-Spring-hw1：Covid19 Cases Prediction | JackWang&#39;s Blog</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 5.4.2">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>



   <style>
    body{
       background-image: url(https://cdn.jsdelivr.net/gh/Tokisaki-Galaxy/res/site/medias/background.jpg);
       background-repeat:no-repeat;
       background-size: 100% 100%;
       background-attachment:fixed;
    }
</style>



<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">JackWang&#39;s Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-book-reader" style="zoom: 0.6;"></i>
      
      <span>博客</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按标签归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按目录归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按日期分类文章</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>



<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">JackWang&#39;s Blog</div>
        <div class="logo-desc">
            
            JackWang的个人博客
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book-reader"></i>
			
			博客
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
                              <span>按标签归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
                              <span>按目录归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
                              <span>按日期分类文章</    span>

                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('抱歉，这篇文章并不想让所有人都看到，请输入授权密码观看')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211207150639649.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">李宏毅ML2021-Spring-hw1：Covid19 Cases Prediction</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Deep-Learning/">
                                <span class="chip bg-color">Deep Learning</span>
                            </a>
                        
                            <a href="/tags/Hungyi-Li/">
                                <span class="chip bg-color">Hungyi Li</span>
                            </a>
                        
                            <a href="/tags/Machine-Learning/">
                                <span class="chip bg-color">Machine Learning</span>
                            </a>
                        
                            <a href="/tags/Neural-Network/">
                                <span class="chip bg-color">Neural Network</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/%E6%9D%8E%E5%AE%8F%E6%AF%85ML2021-Spring-Homeworks/" class="post-category">
                                李宏毅ML2021 Spring Homeworks
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2021-12-11
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2023-06-01
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    7.4k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    37 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <blockquote>
<p>本文主要讲解李宏毅ML2021 Spring的Homework1： Covid19 Cases Prediction</p>
</blockquote>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211207150639649.png" alt="作业一：COVID-19 Cases Prediction"></p>
<h1 id="李宏毅ML2021-Spring-hw1：Covid19-Cases-Prediction"><a href="#李宏毅ML2021-Spring-hw1：Covid19-Cases-Prediction" class="headerlink" title="李宏毅ML2021-Spring-hw1：Covid19 Cases Prediction"></a>李宏毅ML2021-Spring-hw1：Covid19 Cases Prediction</h1><h2 id="1-Homework-Objectives"><a href="#1-Homework-Objectives" class="headerlink" title="1. Homework Objectives"></a>1. Homework Objectives</h2><p>作业一的目的在于：</p>
<ul>
<li>明白如何使用DNN解决Regression问题</li>
<li>掌握基本的DNN训练技术：调参、特征选择、正则化</li>
<li>熟悉Pytorch的使用</li>
</ul>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211207151013750.png" alt="Objectives of Homework1"></p>
<h2 id="2-Task-Description"><a href="#2-Task-Description" class="headerlink" title="2. Task Description"></a>2. Task Description</h2><p>本次任务要求我们对每日的新冠肺炎的确诊人数进行预测。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211207151406811.png" alt="Task Description"></p>
<h3 id="1-Survey-Methods"><a href="#1-Survey-Methods" class="headerlink" title="1. Survey Methods"></a>1. Survey Methods</h3><p>数据集的来源是CMU的研究员在社交媒体上发布调查问卷，问卷的内容包括了心理健康状态、流感类似症状疾病患者人数。而后根据受访者居住的地区来监测美国所有州的所有城市的情况。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211211163552345.png" alt="Survey Method"></p>
<h3 id="2-Data-Inspection"><a href="#2-Data-Inspection" class="headerlink" title="2. Data Inspection"></a>2. Data Inspection</h3><p>本次任务的数据集是csv文件</p>
<p>在训练数据中，训练数据的csv文件中每一行就是一个example。example的前40维是对于城市的one-hot编码，然后每一个样本会给出来三天的调查情况。</p>
<p>其中，<strong>前两天的确诊人数是我们的training的一个feature，而第三天的确诊人数则是我们需要预测的label</strong></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211207151633099.png" alt="Training Data Inspection"></p>
<p>而对于测试数据，则只有前面的feature，没有最后的label，最后我们需要将生成的label以csv文件的形式提交</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211211164704231.png" alt="Testing Data Inspection"></p>
<h3 id="3-Evaluation-Metrics"><a href="#3-Evaluation-Metrics" class="headerlink" title="3. Evaluation Metrics"></a>3. Evaluation Metrics</h3><p>针对本次任务，使用的衡量标准是Rooted Mean Square Error（RMSE），所以最后在kaggle上得到的分数是越低越好。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211207152032106.png" alt="Evaluation Metric"></p>
<h3 id="4-Baselines"><a href="#4-Baselines" class="headerlink" title="4. Baselines"></a>4. Baselines</h3><p>这次任务有三个BaseLine</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211207151811659.png" alt="Task Baseline"></p>
<h2 id="3-My-Solution"><a href="#3-My-Solution" class="headerlink" title="3. My Solution"></a>3. My Solution</h2><p>下面是我关于本次任务的理解和更新</p>
<h3 id="1-Thoughs"><a href="#1-Thoughs" class="headerlink" title="1. Thoughs"></a>1. Thoughs</h3><p>这个是一个regression的任务，最后的输出是一个数字，所以网络最后一层out_feature是1即可</p>
<p>然后这些特征之间有可能有一些特征是和确诊人数的确相关的，有一些是和确诊人数完全无关的，甚至是random的，所以这一些特征可能会影响最后的判断，所以后面需要进行特征选择或者让模型自己选出来重要的特征，然后给重要的特征大的权重。类似于attention</p>
<p>然后可以试试generative的方法，让模型在训练一段时间之后自己生成，即训练判断模型的基础上再生成一个生成模型。生成模型用GAN去生成数据，然后让判断模型去学习。</p>
<p>最后可以试试不确定性的方法。</p>
<h3 id="2-Updates"><a href="#2-Updates" class="headerlink" title="2. Updates"></a>2. Updates</h3><h4 id="2021-12-11：完成了基本框架"><a href="#2021-12-11：完成了基本框架" class="headerlink" title="2021-12-11：完成了基本框架"></a>2021-12-11：完成了基本框架</h4><ul>
<li><strong>进展</strong>：完成了基本的框架的搭建，明天开始训练。完成的框架包括训练代码，网络的定义，dataset和得到提交文件的代码</li>
</ul>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>torch<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:/media/jack/JackCode/project/deeplearning/hw1_1-regression$ tree src/
src/
├── dataset.py
├── gen_submission.py
├── networks.py
├── pathconfig.py
└── train.py
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="2021-12-12-update1：训练时loss不下降-cry"><a href="#2021-12-12-update1：训练时loss不下降-cry" class="headerlink" title="2021-12-12 update1：训练时loss不下降:cry:"></a>2021-12-12 update1：训练时loss不下降<span class="github-emoji"><span>😢</span><img src="https://github.githubassets.com/images/icons/emoji/unicode/1f622.png?v8" aria-hidden="true" onerror="this.parent.classList.add('github-emoji-fallback')"></span></h4><ul>
<li><p><strong>问题</strong>：开始训练，但是训练的时候遇到了一些问题，训练的时候loss一直没有下降，例如下面这张图<img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212115234546.png" alt="loss没有下降"></p>
</li>
<li><p>解决办法：最后经过检查，是batchsize的问题，dataloader的忘记设置batchsize和shuffle了，所以出了问题，这个数据集里单个样本随机波动太大，不像图像是比较通用的，随机波动小，单个example也能学到东西。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212115505406.png" alt="出问题的代码"><br>最后修改掉之后就可以正常训练了，以后一定要注意小batchsize带来的小样本梯度随机波动问题<br><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212115710477.png" alt="修改之后的代码"></p>
<p>最后可以正常训练<img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212115901900.png" alt="正常开始训练"></p>
<p>最后在379个epoch时候停下来了，提交一发<img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212120026808.png" alt="最后的训练结果"></p>
</li>
</ul>
<h4 id="2021-12-12-update2-提交分数起飞-boom"><a href="#2021-12-12-update2-提交分数起飞-boom" class="headerlink" title="2021-12-12 update2: 提交分数起飞:boom:"></a>2021-12-12 update2: 提交分数起飞<span class="github-emoji"><span>💥</span><img src="https://github.githubassets.com/images/icons/emoji/unicode/1f4a5.png?v8" aria-hidden="true" onerror="this.parent.classList.add('github-emoji-fallback')"></span></h4><ul>
<li><p><strong>问题</strong>：接上一个问题，完成了之后提交结果，分数很烂</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212140536683.png" alt="分数很低"></p>
</li>
<li><p><strong>解决办法</strong>：最后经过检查，是网络在训练的时候，对训练数据的feature和label都进行了正则化，使其服从均值为0，标准差为1的分布，这样在训练过程中的梯度的分布也会更加均匀。但是这样做的问题就是网络预测的进过标准化之后的label，所以使用测试数据生成结果的时候得到的也是标准化之后的结果，需要加上label的均值和标准差，但是由于是测试数据，所以没有标准差和均值，而<strong>测试数据和训练数据不是独立同分布的</strong>。所以不能直接加上测试数据的标准差和方差</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212143542126.png" alt="修改前的错误代码，对label也做了标准化"></p>
<p>修改之后不给label做标准化，再训练一次然后提交</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212151141477.png" alt="修改后不对label做标准化"></p>
<p>再进行一次训练</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212151509152.png" alt="修改后的训练"></p>
<p>最后提交的结果，相比于一开始有了很大的进步</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212151837238.png" alt="提交结果"></p>
</li>
</ul>
<h4 id="2021-12-12-update3-Simple-baseline-triangular-flag-on-post"><a href="#2021-12-12-update3-Simple-baseline-triangular-flag-on-post" class="headerlink" title="2021-12-12 update3:  Simple baseline:triangular_flag_on_post:"></a>2021-12-12 update3:  Simple baseline<span class="github-emoji"><span>🚩</span><img src="https://github.githubassets.com/images/icons/emoji/unicode/1f6a9.png?v8" aria-hidden="true" onerror="this.parent.classList.add('github-emoji-fallback')"></span></h4><p>大胜利<span class="github-emoji"><span>🎉</span><img src="https://github.githubassets.com/images/icons/emoji/unicode/1f389.png?v8" aria-hidden="true" onerror="this.parent.classList.add('github-emoji-fallback')"></span></p>
<ul>
<li><p><strong>思路</strong>：尝试了一下仅仅使用前两天的确诊人数作为feature来训练，最后提升巨大，由于我在代码里预留了dim2use，所以直接加上前两的确诊人数的feature和州，一共42个dimension再来一次</p>
<p><strong>所以前面的猜想确实成立，即存在一些和确诊人数完全无关的特征，这些特征会极大地影响模型的表现</strong></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212155208705.png" alt="加上前两天的确诊人数"></p>
</li>
<li><p><strong>效果</strong>：</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212155232450.png" alt="巨大的进步"></p>
</li>
</ul>
<h4 id="2021-12-12-update4-特征选择的思考-Medium-Baseline-triangular-flag-on-post"><a href="#2021-12-12-update4-特征选择的思考-Medium-Baseline-triangular-flag-on-post" class="headerlink" title="2021-12-12 update4: 特征选择的思考 Medium Baseline :triangular_flag_on_post:"></a>2021-12-12 update4: 特征选择的思考 Medium Baseline <span class="github-emoji"><span>🚩</span><img src="https://github.githubassets.com/images/icons/emoji/unicode/1f6a9.png?v8" aria-hidden="true" onerror="this.parent.classList.add('github-emoji-fallback')"></span></h4><ul>
<li><p><strong>想法</strong>：因为在前面使用了州和前两天，得到的分数非常的高，所以利用了特征选择的手段，试了一下不同的特征，得到的结果如下</p>
<ul>
<li><p><strong>使用了州和前两天</strong></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212163655980.png" alt="使用州和前两天"></p>
</li>
<li><p><strong>使用了得分前15的特征</strong></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212163737608.png" alt="得分前15的特征"></p>
</li>
<li><p><strong>使用了得分大于500的特征</strong></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212163814031.png" alt="得分大于500的特征"></p>
</li>
<li><p><strong>只使用前两天得病的人数</strong></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212163858827.png" alt="只使用前两天得病的人数"></p>
</li>
</ul>
<p>而特征选择时候前90个特征的得分如下，可以看到，前两个特征（前两天确诊的人数）的分数是远远大于剩下的特征，也就难怪训练出来最后一个的得分最高</p>
<pre class="line-numbers language-bash"><code class="language-bash">                    Specs          Score
76       tested_positive.1  148069.658278
58         tested_positive   69603.872591
43            hh_cmnty_cli    9235.492094
61          hh_cmnty_cli.1    9209.019558
79          hh_cmnty_cli.2    9097.375172
44          nohh_cmnty_cli    8395.421300
62        nohh_cmnty_cli.1    8343.255927
80        nohh_cmnty_cli.2    8208.176435
41                     cli    6388.906849
59                   cli.1    6374.548000
77                   cli.2    6250.008702
42                     ili    5998.922880
60                   ili.1    5937.588576
78                   ili.2    5796.947672
93      worried_finances.2     833.613191
75      worried_finances.1     811.916460
57        worried_finances     788.076931
88        public_transit.2     686.736539
70        public_transit.1     681.562902
52          public_transit     678.834789
84                  shop.2     561.764051
66                  shop.1     553.876727
48                    shop     546.553395
0                       <span class="token function">id</span>     328.682311
92    worried_become_ill.2     208.122647
18                      MA     205.672603
74    worried_become_ill.1     203.473072
56      worried_become_ill     199.195461
86            spent_time.2     193.926689
68            spent_time.1     188.763751
50              spent_time     183.145229
21                      MS     164.450807
30                      OK     160.776135
7                       CT     146.609540
17                      MD     123.546972
5                       CA     122.361562
29                      OH      92.603763
24                      NV      86.480581
10                      ID      85.604275
53                 anxious      83.532232
22                      MO      80.571861
36                      UT      76.092379
71               anxious.1      75.073522
3                       AZ      72.803676
85            restaurant.2      71.047754
67            restaurant.1      70.086743
23                      NE      69.082676
49              restaurant      69.027593
35                      TX      68.609347
89               anxious.2      64.587744
2                       AK      64.396242
32                      PA      63.096093
6                       CO      47.446096
15                      KY      43.151972
13                      IA      42.096600
8                       FL      39.976778
27                      NY      38.261684
38                      WA      35.198775
31                      OR      30.424819
82  travel_outside_state.2      29.424808
19                      MI      27.611294
64  travel_outside_state.1      27.477274
46    travel_outside_state      25.788251
14                      KS      18.699994
55           felt_isolated      18.345728
33                      RI      18.223800
73         felt_isolated.1      18.050098
91         felt_isolated.2      17.895720
34                      SC      15.174100
45            wearing_mask      13.106862
63          wearing_mask.1      11.660815
20                      MN      11.134685
87           large_event.2      10.995323
1                       AL      10.915952
81          wearing_mask.2      10.423544
39                      WV       9.407003
69           large_event.1       9.131669
25                      NJ       8.637824
37                      VA       8.328449
51             large_event       7.449227
9                       GA       7.300447
83     work_outside_home.2       4.174404
65     work_outside_home.1       3.887156
54               depressed       3.837879
11                      IL       3.834123
47       work_outside_home       3.283513
72             depressed.1       2.967971
28                      NC       2.877359
4                       AR       2.546223
90             depressed.2       2.362492
Int64Index<span class="token punctuation">(</span><span class="token punctuation">[</span>76, 58, 43, 61, 79, 44, 62, 80, 41, 59, 77, 42, 60, 78, 93, 75, 57,
            88, 70, 52, 84, 66, 48,  0, 92, 18, 74, 56, 86, 68, 50, 21, 30,  7,
            17,  5, 29, 24, 10, 53, 22, 36, 71,  3, 85, 67, 23, 49, 35, 89,  2,
            32,  6, 15, 13,  8, 27, 38, 31, 82, 19, 64, 46, 14, 55, 33, 73, 91,
            34, 45, 63, 20, 87,  1, 81, 39, 69, 25, 37, 51,  9, 83, 65, 54, 11,
            47, 72, 28,  4, 90<span class="token punctuation">]</span>,
           dtype<span class="token operator">=</span><span class="token string">'int64'</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
</ul>
<h4 id="2021-12-12-update5-大进步-tada"><a href="#2021-12-12-update5-大进步-tada" class="headerlink" title="2021-12-12 update5: 大进步:tada:"></a>2021-12-12 update5: 大进步<span class="github-emoji"><span>🎉</span><img src="https://github.githubassets.com/images/icons/emoji/unicode/1f389.png?v8" aria-hidden="true" onerror="this.parent.classList.add('github-emoji-fallback')"></span></h4><ul>
<li><p><strong>思路</strong>：因为前面实验了一下使用不同的特征，发现只有前两个特征，所以我感觉特征选择可能走到了头，需要新的思路来进一步提升模型的表现。因此从模型本身下手，更换了网络模型，从助教的baseline换成了残差模型，同样还是只有两个特征</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212170623668.png" alt="网络模型"></p>
</li>
<li><p><strong>效果</strong>：最后的效果的提升还是巨大的</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212170744439.png" alt="提交后的结果"></p>
</li>
</ul>
<h4 id="2021-12-12-update6-关于优化器"><a href="#2021-12-12-update6-关于优化器" class="headerlink" title="2021-12-12 update6: 关于优化器"></a>2021-12-12 update6: 关于优化器</h4><ul>
<li><p><strong>思路</strong>：前面改了一下网络结构，使得精度有了不错的提升。我在特征选择后面又想到了一个思路，就是换一个优化器，所以在实验完网络结构之后我又实验了一下优化器，看看优化器会不会对训练又帮助，所以把SGD换成了AdamW，AdamW比Adam少了对bias的求导，所以方向避免出错。</p>
<p>换优化器其实我也是实验了不少次，因为使用SGD的时候，不管超参数怎么变，总会在51个epoch的时候出现一次最优值。虽然有的超参数经过比较长的epoch之后会有新的最优值，但是51这个epoch常常会出现，不管我怎么变超参数。</p>
<p>所以我怀疑51个epoch的时候对于SGD优化器，已经到了一个比较wide的谷底，这个时候很难跳出去，所以有的时候会直接在这里停下来。</p>
<p>作为弥补我换了一个优化器，换成了AdamW，因为AdamW跳出saddle point的能力要强不少</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212173038313.png" alt="一些训练轮次"></p>
</li>
<li><p><strong>结果</strong>：更换优化器之后，训练轮次要长很多，而且最初的十多个epoch会有震荡，还在loss 空间的高处</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/train_log.png" alt="AdamW的训练过程"></p>
<p>上面的图其实看不太出来，因为横轴30000多个step，但是SGD的训练图在开始部分是直线下冲的</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/train_log1.png" alt="train_log1"></p>
<p>最后的结果是成功冲进了1分以内的误差</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212173834843.png" alt="1分以内"></p>
</li>
</ul>
<h4 id="2012-12-12-update7-关于正则化"><a href="#2012-12-12-update7-关于正则化" class="headerlink" title="2012-12-12 update7: 关于正则化"></a>2012-12-12 update7: 关于正则化</h4><ul>
<li><p><strong>思路</strong>：前面在想的时候其实还想到了第三个思路，就是在网络中加入正则化方法，一个是加入dropout，一个是加入batch normalization，因为目前这两个是公认的不错的的正则化方法。而使用正则化方法能够帮助加速训练、提高收敛精度。然而在训练时候却发现不太对劲。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212175933758.png" alt="不太对劲的正则化"></p>
<p>可以看到，使用batch norm 和 dropout，收敛速度不降反升。这个和大家的公认有所差别，我百思不得其解<span class="github-emoji"><span>😕</span><img src="https://github.githubassets.com/images/icons/emoji/unicode/1f615.png?v8" aria-hidden="true" onerror="this.parent.classList.add('github-emoji-fallback')"></span></p>
<p>最后怀疑是AdamW优化器的问题。因为AdamW优化器中有限制梯度的项，而dropout会mask掉一些神经元，导致梯度在这里为0，而batch norm则是炸平梯度空间。所以怀疑问题出在这里，优化器换回SGD，好家伙，这个速度，比纯SGD快多了（下面的图第一个日志写错了，应该是batch norm）</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211212180324925.png" alt="使用AdamW的训练记录"></p>
</li>
</ul>
<h4 id="2021-12-13-update1-Strong-Network，More-Complex-data"><a href="#2021-12-13-update1-Strong-Network，More-Complex-data" class="headerlink" title="2021-12-13 update1: Strong Network，More Complex data"></a>2021-12-13 update1: Strong Network，More Complex data</h4><ul>
<li><p><strong>问题</strong>：昨天在最后使用adam优化器和residual模块的配合，训练阶段使用特征选择选出来的前两天的确诊人数，一共2个dimension的feature作为输入进行训练，最后得到的精度0.98左右。昨天的炼丹就到此为止了。昨天晚上在复盘的时候想到，老师上课讲过的模型训练攻略里说的内容。</p>
<ol>
<li>而在复盘的时候想起来，昨天的炼丹过程其实提升最大的有两段，第一段就是在一开始使用助教的baseline的时候，使用特征抽取选取了前两天的特征，loss在training data上的损失就收敛到1.x左右了。可是使用baseline在这之后不管怎么样都始终无法进入到0.x的大关。而关于Optimization中也实验过了正则化、调参、换优化器等等手段。但是模型的表现还是没有下降。</li>
<li>第二个阶段就是换了residual的模型，性能立马又有了提升。但是同样的，residual的模型各种optimization的方法都已经试过了，最后还是卡在了0.98左右。</li>
</ol>
<p>最后想想老师讲的课，突然明白，在一开始的时候，model的能力不够强，所以没有办法学好14个重要特征。因此选择前两天的确诊人数作为feature，性能有了很大的提升。这个时候的问题可能是baseline model 的model bias了，即模型的能力不够强。后来歪打正着换了一个模型，同样的数据和参数下训练，性能立马有了提升，所以确信是baseline模型的问题。</p>
<p>可是后来不管怎么调，residual的模型性能也上不去了。后来其实我也试了一下wide的模型，wide + residual的模型，最后性能都不是很好。这个时候我突然想起来，因为baseline里面是两个feature表现最好，所以我就直接用了两个dimension的feature来train residual的一系列model。后来换model单纯的只是希望能够得到能力更强的model。</p>
<p>但是问题却在于，能力更强的model确实能够得到更好的performance，但是我并不知道residual是否达到了他的上限，即有可能是我只用了前两天的确诊人数，这样的training data对residual太弱了，他完全学有余力。</p>
<p>刚好我也没有像baseline一样进行特征选择的测试，所以我其实并不知道residual的model是否达到他的上限了。所以今天我试着使用了更多的training data，即使用了所有大于5000分的数据，一共14个维度</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211213003527934.png" alt="用更多的数据来训练residual model"></p>
</li>
<li><p><strong>结果</strong>：最后经过训练之后提交，大进步<span class="github-emoji"><span>🎉</span><img src="https://github.githubassets.com/images/icons/emoji/unicode/1f389.png?v8" aria-hidden="true" onerror="this.parent.classList.add('github-emoji-fallback')"></span></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211213003658575.png" alt="使用14个feature训练的结果"></p>
</li>
</ul>
<h4 id="2021-12-13-update2-temp-final"><a href="#2021-12-13-update2-temp-final" class="headerlink" title="2021-12-13 update2: temp final"></a>2021-12-13 update2: temp final</h4><p>搞到现在，做了一下，现在最优的成绩是下面的，基本上就是残差模块的数量问题，关于伪标签其实我感觉如果用test的data来做的话，就会有过拟合test数据的嫌疑。所以目前想的是等用generative的方法来做，目前暂时就先到这里</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211213015919490.png" alt="目前的最优成绩"></p>
<h2 id="4-My-Codes"><a href="#4-My-Codes" class="headerlink" title="4. My Codes"></a>4. My Codes</h2><p>下面是我的代码</p>
<h3 id="1-代码结构"><a href="#1-代码结构" class="headerlink" title="1. 代码结构"></a>1. 代码结构</h3><p>整个代码的结构如下</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>torch<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:/media/jack/JackCode/project/deeplearning/hw1_1-regression$ tree -d -L 2
<span class="token keyword">.</span>
├── checkpoint
│   ├── Baseline
│   ├── DeeperNet
│   ├── DeeperNormalizedNet
│   ├── OtherNet
│   └── WideNet
├── data
├── log
│   ├── Baseline
│   ├── DeeperNet
│   ├── DeeperNormalizedNet
│   ├── OtherNet
│   └── WideNet
├── src
│   ├── config
│   └── __pycache__
└── submission
    ├── Baseline
    ├── DeeperNet
    ├── DeeperNormalizedNet
    ├── OtherNet
    └── WideNet

22 directories
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>其中:</p>
<ul>
<li><p>src里都是源代码</p>
</li>
<li><p>checkpoint、log和submission都如其名</p>
</li>
<li><p>每次进行训练的时候，都会根据模型（网络）名创建一个文件夹，在里面放对应的训练日志、提交结果、参数文件等等。以log为例，每次得到的结果都是根据日期和时间创建的文件夹。只有正确训练结束才会三个文件，分别为训练摘要、训练图像以及训练的过程中的loss。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>torch<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:/media/jack/JackCode/project/deeplearning/hw1_1-regression$ tree log/DeeperNet/
log/DeeperNet/
├── 2021-12-12 16_52_47
├── 2021-12-12 16_53_13
├── 2021-12-12 16_53_38
├── 2021-12-12 16_54_21
├── 2021-12-12 16_55_03
│   └── message.txt
├── 2021-12-12 16_56_25
│   └── message.txt
├── 2021-12-12 16_56_50
│   ├── message.txt
│   ├── train_log1.png
│   └── train_val.loss.pkl
├── 2021-12-12 17_12_44
├── 2021-12-12 17_12_57
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 17_16_00
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 17_16_56
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 17_17_49
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 17_20_12
├── 2021-12-12 17_20_34
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 17_41_30
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 18_17_42
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 18_21_58
├── 2021-12-12 18_22_24
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 18_28_27
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 19_17_44
│   └── message.txt
├── 2021-12-12 19_18_15
│   └── message.txt
├── 2021-12-12 19_18_42
│   └── message.txt
├── 2021-12-12 19_19_12
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 19_20_16
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 19_25_46
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 19_27_36
│   └── message.txt
├── 2021-12-12 19_28_20
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 19_33_37
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 19_37_51
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 19_42_24
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 19_48_44
│   └── message.txt
├── 2021-12-12 19_48_59
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 19_52_28
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 19_56_30
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 20_05_07
├── 2021-12-12 20_05_46
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 20_10_02
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 20_12_20
│   └── message.txt
├── 2021-12-12 20_13_33
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 20_16_43
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-12 20_21_37
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-13 00_46_42
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-13 00_59_08
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-13 01_34_47
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-13 01_39_11
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-13 01_48_12
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
├── 2021-12-13 01_51_22
│   ├── message.txt
│   ├── train_log.png
│   └── train_val.loss.pkl
└── 2021-12-13 01_55_10
    ├── message.txt
    ├── train_log.png
    └── train_val.loss.pkl

48 directories, 104 files
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
</ul>
<ul>
<li>直接提交submission中生成的文件即可</li>
</ul>
<h3 id="2-src中文件作用"><a href="#2-src中文件作用" class="headerlink" title="2. src中文件作用"></a>2. src中文件作用</h3><p>src中有这些文件</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>torch<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:/media/jack/JackCode/project/deeplearning/hw1_1-regression$ tree src/ -L 1
src/
├── config
├── dataset.py
├── feature_selection.py
├── gen_submission.py
├── networks.py
├── pathconfig.py
├── __pycache__
└── train.py

2 directories, 6 files
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>其中：</p>
<ul>
<li>train是训练代码</li>
<li>gen_submission是生成结果的代码</li>
<li>networks是所有的网络的代码</li>
<li>feature_selection 是做特征工程的时候代码</li>
</ul>
<p>具体的代码在下面给出来</p>
<h3 id="3-data中的数据"><a href="#3-data中的数据" class="headerlink" title="3. data中的数据"></a>3. data中的数据</h3><p>data中需要把训练、测试数据放进去就行了</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>torch<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:/media/jack/JackCode/project/deeplearning/hw1_1-regression$ tree data/
data/
├── covid.test.csv
├── covid.train.csv
└── sampleSubmission.csv

0 directories, 3 files
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>其实最后只会用到test 和 train</p>
<h3 id="4-代码"><a href="#4-代码" class="headerlink" title="4. 代码"></a>4. 代码</h3><p>用的时候要注意，gen_submission要改参数文件的路径。此外自己想写网络的话需要继承_NetworkBase，然后重写dtype</p>
<p>训练网络和生成结果的时候一种网络一个函数即可</p>
<h4 id="1-pathconfig-py"><a href="#1-pathconfig-py" class="headerlink" title="1. pathconfig.py"></a>1. pathconfig.py</h4><p>自动决定所有的路径</p>
<pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">import</span> sys
<span class="token keyword">from</span> pathlib <span class="token keyword">import</span> Path

<span class="token keyword">from</span> colorama <span class="token keyword">import</span> Fore<span class="token punctuation">,</span> Style


<span class="token keyword">if</span> <span class="token punctuation">(</span>p<span class="token punctuation">:</span><span class="token operator">=</span>Path<span class="token punctuation">(</span>__file__<span class="token punctuation">)</span><span class="token punctuation">.</span>resolve<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>parent<span class="token punctuation">)</span><span class="token punctuation">.</span>__str__<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">not</span> <span class="token keyword">in</span> sys<span class="token punctuation">.</span>path<span class="token punctuation">:</span>
    sys<span class="token punctuation">.</span>path<span class="token punctuation">.</span>append<span class="token punctuation">(</span>p<span class="token punctuation">.</span>__str__<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">green</span><span class="token punctuation">(</span>s<span class="token punctuation">:</span> str<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> f<span class="token string">"{Fore.GREEN}{s}{Fore.RESET}"</span>

<span class="token keyword">def</span> <span class="token function">red</span><span class="token punctuation">(</span>s<span class="token punctuation">:</span> str<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> f<span class="token string">"{Fore.RED}{s}{Fore.RESET}"</span>

<span class="token keyword">def</span> <span class="token function">yellow</span><span class="token punctuation">(</span>s<span class="token punctuation">:</span> str<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> f<span class="token string">"{Fore.YELLOW}{s}{Fore.RESET}"</span>


<span class="token keyword">class</span> <span class="token class-name">Paths</span><span class="token punctuation">:</span>
    base_path<span class="token punctuation">:</span> Path <span class="token operator">=</span> Path<span class="token punctuation">(</span>__file__<span class="token punctuation">)</span><span class="token punctuation">.</span>resolve<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>parents<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
    src_path<span class="token punctuation">:</span> Path <span class="token operator">=</span> base_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"src"</span><span class="token punctuation">)</span>
    log_path<span class="token punctuation">:</span> Path <span class="token operator">=</span> base_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"log"</span><span class="token punctuation">)</span>
    data_path<span class="token punctuation">:</span> Path <span class="token operator">=</span> base_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">)</span>
    checkpoint_path<span class="token punctuation">:</span> Path <span class="token operator">=</span> base_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"checkpoint"</span><span class="token punctuation">)</span>
    submission_path<span class="token punctuation">:</span> Path <span class="token operator">=</span> base_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"submission"</span><span class="token punctuation">)</span>

    train_path<span class="token punctuation">:</span> Path <span class="token operator">=</span> data_path <span class="token operator">/</span> <span class="token string">"covid.train.csv"</span>
    test_path<span class="token punctuation">:</span> Path <span class="token operator">=</span> data_path <span class="token operator">/</span> <span class="token string">"covid.test.csv"</span>

    config_path<span class="token punctuation">:</span> Path <span class="token operator">=</span> src_path <span class="token operator">/</span> <span class="token string">"config"</span>

<span class="token keyword">for</span> key<span class="token punctuation">,</span> value <span class="token keyword">in</span> Paths<span class="token punctuation">.</span>__dict__<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> key<span class="token punctuation">.</span>endswith<span class="token punctuation">(</span><span class="token string">"_path"</span><span class="token punctuation">)</span> <span class="token operator">and</span> <span class="token operator">not</span> value<span class="token punctuation">.</span>exists<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        value<span class="token punctuation">.</span>mkdir<span class="token punctuation">(</span>parents<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token punctuation">:</span>
    pc <span class="token operator">=</span> Paths<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>pc<span class="token punctuation">.</span>base_path<span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="2-feature-selection-py"><a href="#2-feature-selection-py" class="headerlink" title="2. feature_selection.py"></a>2. feature_selection.py</h4><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd
<span class="token keyword">from</span> sklearn <span class="token keyword">import</span> preprocessing
<span class="token keyword">from</span> sklearn<span class="token punctuation">.</span>feature_selection <span class="token keyword">import</span> SelectKBest<span class="token punctuation">,</span> f_regression

<span class="token keyword">from</span> pathconfig <span class="token keyword">import</span> Paths

data <span class="token operator">=</span> pd<span class="token punctuation">.</span>read_csv<span class="token punctuation">(</span>Paths<span class="token punctuation">.</span>train_path<span class="token punctuation">)</span>
x<span class="token punctuation">,</span> y <span class="token operator">=</span> data<span class="token punctuation">.</span>iloc<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> data<span class="token punctuation">.</span>iloc<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>


x <span class="token operator">=</span> <span class="token punctuation">(</span>x <span class="token operator">-</span> x<span class="token punctuation">.</span>min<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span>x<span class="token punctuation">.</span>max<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> x<span class="token punctuation">.</span>min<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 根据得分函数计算出来的分数选取前五个</span>
bestfeatures <span class="token operator">=</span> SelectKBest<span class="token punctuation">(</span>score_func<span class="token operator">=</span>f_regression<span class="token punctuation">,</span> k<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span>
fit <span class="token operator">=</span> bestfeatures<span class="token punctuation">.</span>fit<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
dfscores <span class="token operator">=</span> pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">(</span>fit<span class="token punctuation">.</span>scores_<span class="token punctuation">)</span>
dfcolumns <span class="token operator">=</span> pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">(</span>x<span class="token punctuation">.</span>columns<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">#concat two dataframes for better visualization </span>
featureScores <span class="token operator">=</span> pd<span class="token punctuation">.</span>concat<span class="token punctuation">(</span><span class="token punctuation">[</span>dfcolumns<span class="token punctuation">,</span>dfscores<span class="token punctuation">]</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
featureScores<span class="token punctuation">.</span>columns <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'Specs'</span><span class="token punctuation">,</span><span class="token string">'Score'</span><span class="token punctuation">]</span>  <span class="token comment" spellcheck="true">#naming the dataframe columns</span>
pd<span class="token punctuation">.</span>set_option<span class="token punctuation">(</span><span class="token string">'display.max_rows'</span><span class="token punctuation">,</span> None<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>featureScores<span class="token punctuation">.</span>nlargest<span class="token punctuation">(</span><span class="token number">90</span><span class="token punctuation">,</span><span class="token string">'Score'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true">#print 15 best features</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>featureScores<span class="token punctuation">.</span>nlargest<span class="token punctuation">(</span><span class="token number">90</span><span class="token punctuation">,</span> <span class="token string">"Score"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>index<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>pd<span class="token punctuation">.</span>Series<span class="token punctuation">(</span>data<span class="token punctuation">.</span>columns<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="3-dataset-py"><a href="#3-dataset-py" class="headerlink" title="3. dataset.py"></a>3. dataset.py</h4><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">import</span> pickle
<span class="token keyword">import</span> random
<span class="token keyword">import</span> typing

<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd

<span class="token keyword">import</span> torch<span class="token punctuation">.</span>utils<span class="token punctuation">.</span>data <span class="token keyword">as</span> data

<span class="token keyword">from</span> pathconfig <span class="token keyword">import</span> <span class="token operator">*</span>


<span class="token keyword">class</span> <span class="token class-name">CovidDataset</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span>Dataset<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> split<span class="token operator">=</span><span class="token string">"train"</span><span class="token punctuation">,</span> dim2use<span class="token punctuation">:</span> typing<span class="token punctuation">.</span>Iterable<span class="token punctuation">[</span>int<span class="token punctuation">]</span><span class="token operator">=</span>None<span class="token punctuation">,</span> val_ratio<span class="token punctuation">:</span> float<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> None<span class="token punctuation">:</span>
        super<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        dim2use <span class="token operator">=</span> range<span class="token punctuation">(</span><span class="token number">93</span><span class="token punctuation">)</span> <span class="token keyword">if</span> dim2use <span class="token keyword">is</span> None <span class="token keyword">else</span> dim2use
        <span class="token keyword">assert</span> split <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token string">"train"</span><span class="token punctuation">,</span> <span class="token string">"test"</span><span class="token punctuation">,</span> <span class="token string">"val"</span><span class="token punctuation">]</span><span class="token punctuation">,</span> red<span class="token punctuation">(</span>f<span class="token string">"无效的数据集类别：{split}"</span><span class="token punctuation">)</span>
        <span class="token keyword">assert</span> <span class="token punctuation">(</span>t<span class="token punctuation">:</span><span class="token operator">=</span><span class="token punctuation">(</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>dim2use<span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">93</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>all<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> red<span class="token punctuation">(</span>f<span class="token string">"无效的feature dimension， 最大只有 93 维(0-92), dim2use={dim2use[t]} > 92"</span><span class="token punctuation">)</span>

        <span class="token comment" spellcheck="true"># read file</span>
        self<span class="token punctuation">.</span>split<span class="token punctuation">:</span> str <span class="token operator">=</span> split
        <span class="token keyword">if</span> self<span class="token punctuation">.</span>split <span class="token operator">==</span> <span class="token string">"test"</span><span class="token punctuation">:</span>
            raw_csv<span class="token punctuation">:</span> np<span class="token punctuation">.</span>ndarray <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">:</span><span class="token operator">=</span>pd<span class="token punctuation">.</span>read_csv<span class="token punctuation">(</span>Paths<span class="token punctuation">.</span>test_path<span class="token punctuation">,</span> header<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> index_col<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> dtype<span class="token operator">=</span>float<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>to_numpy<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            raw_csv<span class="token punctuation">:</span> np<span class="token punctuation">.</span>ndarray <span class="token operator">=</span> pd<span class="token punctuation">.</span>read_csv<span class="token punctuation">(</span>Paths<span class="token punctuation">.</span>train_path<span class="token punctuation">,</span> header<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> index_col<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> dtype<span class="token operator">=</span>float<span class="token punctuation">)</span><span class="token punctuation">.</span>to_numpy<span class="token punctuation">(</span><span class="token punctuation">)</span>

            <span class="token keyword">if</span> <span class="token number">0</span> <span class="token operator">&lt;</span> val_ratio <span class="token operator">&lt;</span> <span class="token number">1</span><span class="token punctuation">:</span>
                val_idx<span class="token punctuation">:</span> typing<span class="token punctuation">.</span>List<span class="token punctuation">[</span>int<span class="token punctuation">]</span> <span class="token operator">=</span> random<span class="token punctuation">.</span>sample<span class="token punctuation">(</span>population<span class="token operator">=</span>range<span class="token punctuation">(</span>len<span class="token punctuation">(</span>raw_csv<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> k<span class="token operator">=</span>int<span class="token punctuation">(</span>val_ratio <span class="token operator">*</span> len<span class="token punctuation">(</span>raw_csv<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                train_idx<span class="token punctuation">:</span> typing<span class="token punctuation">.</span>List<span class="token punctuation">[</span>int<span class="token punctuation">]</span> <span class="token operator">=</span> list<span class="token punctuation">(</span>set<span class="token punctuation">(</span>range<span class="token punctuation">(</span>len<span class="token punctuation">(</span>raw_csv<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span> set<span class="token punctuation">(</span>val_idx<span class="token punctuation">)</span><span class="token punctuation">)</span>
                <span class="token keyword">if</span> Paths<span class="token punctuation">.</span>config_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"train_val.pkl"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>exists<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                    <span class="token keyword">print</span><span class="token punctuation">(</span>yellow<span class="token punctuation">(</span><span class="token string">"覆盖train_val.pkl"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                <span class="token keyword">with</span> open<span class="token punctuation">(</span>Paths<span class="token punctuation">.</span>config_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"train_val.pkl"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"wb"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
                    pickle<span class="token punctuation">.</span>dump<span class="token punctuation">(</span>obj<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">"train"</span><span class="token punctuation">:</span>train_idx<span class="token punctuation">,</span> <span class="token string">"val"</span><span class="token punctuation">:</span>val_idx<span class="token punctuation">}</span><span class="token punctuation">,</span> file<span class="token operator">=</span>f<span class="token punctuation">)</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                <span class="token keyword">try</span><span class="token punctuation">:</span>
                    <span class="token keyword">with</span> open<span class="token punctuation">(</span>Paths<span class="token punctuation">.</span>config_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"train_val.pkl"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"rb"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
                        p <span class="token operator">=</span> pickle<span class="token punctuation">.</span>load<span class="token punctuation">(</span>file<span class="token operator">=</span>f<span class="token punctuation">)</span>
                        train_idx<span class="token punctuation">:</span> typing<span class="token punctuation">.</span>List<span class="token punctuation">[</span>int<span class="token punctuation">]</span> <span class="token operator">=</span> p<span class="token punctuation">[</span><span class="token string">"train"</span><span class="token punctuation">]</span>
                        val_idx<span class="token punctuation">:</span> typing<span class="token punctuation">.</span>List<span class="token punctuation">[</span>int<span class="token punctuation">]</span> <span class="token operator">=</span> p<span class="token punctuation">[</span><span class="token string">"val"</span><span class="token punctuation">]</span>
                <span class="token keyword">except</span> FileNotFoundError<span class="token punctuation">:</span>
                    <span class="token keyword">assert</span> <span class="token boolean">False</span><span class="token punctuation">,</span> red<span class="token punctuation">(</span>f<span class="token string">"不存在train_validation的config文件，请先指定val_ratio生成train_val的config"</span><span class="token punctuation">)</span>

            <span class="token keyword">if</span> split <span class="token operator">==</span> <span class="token string">"val"</span><span class="token punctuation">:</span>
                raw_csv <span class="token operator">=</span> raw_csv<span class="token punctuation">[</span>val_idx<span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                raw_csv <span class="token operator">=</span> raw_csv<span class="token punctuation">[</span>train_idx<span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span>

        <span class="token comment" spellcheck="true"># standard</span>
        self<span class="token punctuation">.</span>mean <span class="token operator">=</span> raw_csv<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>mean<span class="token punctuation">(</span>axis<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>std <span class="token operator">=</span> raw_csv<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>std<span class="token punctuation">(</span>axis<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
        raw_csv<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>raw_csv<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">-</span> self<span class="token punctuation">.</span>mean<span class="token punctuation">)</span> <span class="token operator">/</span> self<span class="token punctuation">.</span>std
        <span class="token keyword">if</span> self<span class="token punctuation">.</span>split <span class="token operator">!=</span> <span class="token string">"test"</span><span class="token punctuation">:</span>
            self<span class="token punctuation">.</span>data<span class="token punctuation">:</span> np<span class="token punctuation">.</span>ndarray <span class="token operator">=</span> raw_csv<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> dim2use<span class="token punctuation">]</span>
            self<span class="token punctuation">.</span>label<span class="token punctuation">:</span> np<span class="token punctuation">.</span>ndarray <span class="token operator">=</span> raw_csv<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            self<span class="token punctuation">.</span>data<span class="token punctuation">:</span> np<span class="token punctuation">.</span>ndarray <span class="token operator">=</span> raw_csv<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> dim2use<span class="token punctuation">]</span>

    <span class="token keyword">def</span> <span class="token function">__len__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> int<span class="token punctuation">:</span>
        <span class="token keyword">return</span> len<span class="token punctuation">(</span>self<span class="token punctuation">.</span>data<span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">__getitem__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> index<span class="token punctuation">:</span> int<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> typing<span class="token punctuation">.</span>Tuple<span class="token punctuation">[</span>np<span class="token punctuation">.</span>ndarray<span class="token punctuation">]</span><span class="token punctuation">:</span>
        <span class="token keyword">if</span> self<span class="token punctuation">.</span>split <span class="token operator">!=</span> <span class="token string">"test"</span><span class="token punctuation">:</span>
            <span class="token keyword">return</span> self<span class="token punctuation">.</span>data<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">,</span> self<span class="token punctuation">.</span>label<span class="token punctuation">[</span>index<span class="token punctuation">]</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            <span class="token keyword">return</span> self<span class="token punctuation">.</span>data<span class="token punctuation">[</span>index<span class="token punctuation">]</span>




<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># cd = CovidDataset(split="train", val_ratio=0.1)</span>
    <span class="token comment" spellcheck="true"># cd = CovidDataset(split="val", dim2use=range(10))</span>
    <span class="token comment" spellcheck="true"># cd = CovidDataset(split="test", dim2use=range(10))</span>
    cd <span class="token operator">=</span> CovidDataset<span class="token punctuation">(</span>split<span class="token operator">=</span><span class="token string">"train"</span><span class="token punctuation">,</span> dim2use<span class="token operator">=</span>None<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>cd<span class="token punctuation">.</span>data<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
    <span class="token keyword">for</span> x<span class="token punctuation">,</span> y <span class="token keyword">in</span> cd<span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>x<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="4-networks-py"><a href="#4-networks-py" class="headerlink" title="4. networks.py"></a>4. networks.py</h4><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">import</span> torch
<span class="token keyword">import</span> torch<span class="token punctuation">.</span>nn <span class="token keyword">as</span> nn


<span class="token keyword">class</span> <span class="token class-name">_NetworkBase</span><span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Module<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        super<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>dtype <span class="token operator">=</span> None
        self<span class="token punctuation">.</span>net<span class="token punctuation">:</span> nn<span class="token punctuation">.</span>Module <span class="token operator">=</span> None


<span class="token keyword">class</span> <span class="token class-name">Baseline</span><span class="token punctuation">(</span>_NetworkBase<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> in_features<span class="token punctuation">:</span>int<span class="token punctuation">)</span><span class="token punctuation">:</span>
        super<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>net <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span>in_features<span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span>

        <span class="token keyword">for</span> name<span class="token punctuation">,</span> son <span class="token keyword">in</span> self<span class="token punctuation">.</span>net<span class="token punctuation">.</span>named_children<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            weight<span class="token punctuation">:</span> nn<span class="token punctuation">.</span>parameter<span class="token punctuation">.</span>Parameter
            <span class="token keyword">if</span> <span class="token punctuation">(</span>weight<span class="token punctuation">:</span><span class="token operator">=</span>getattr<span class="token punctuation">(</span>son<span class="token punctuation">,</span> <span class="token string">"weight"</span><span class="token punctuation">,</span> None<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token operator">not</span> None<span class="token punctuation">:</span>
                self<span class="token punctuation">.</span>dtype<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>dtype <span class="token operator">=</span> weight<span class="token punctuation">.</span>dtype


    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true"># x: [batch, channel]</span>
        <span class="token keyword">return</span> self<span class="token punctuation">.</span>net<span class="token punctuation">(</span>x<span class="token punctuation">)</span>


<span class="token keyword">class</span> <span class="token class-name">DeeperNet</span><span class="token punctuation">(</span>_NetworkBase<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> in_features<span class="token punctuation">:</span> int<span class="token punctuation">)</span><span class="token punctuation">:</span>
        super<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>input_trainsform <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span>in_features<span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>LeakyReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=128),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
        <span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>residual_block1 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>LeakyReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=64),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>LeakyReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=128),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
        <span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>residual_block2 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>LeakyReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=64),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>LeakyReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=128),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
        <span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>residual_block3 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>LeakyReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=64),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>LeakyReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=128),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
        <span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>residual_block4 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>LeakyReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=64),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>LeakyReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=128),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
        <span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>output <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.LeakyReLU(),</span>
            <span class="token comment" spellcheck="true"># nn.Linear(in_features=32, out_features=1)</span>
        <span class="token punctuation">)</span>

        <span class="token keyword">for</span> name<span class="token punctuation">,</span> son <span class="token keyword">in</span> self<span class="token punctuation">.</span>input_trainsform<span class="token punctuation">.</span>named_children<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            weight<span class="token punctuation">:</span> nn<span class="token punctuation">.</span>parameter<span class="token punctuation">.</span>Parameter
            <span class="token keyword">if</span> <span class="token punctuation">(</span>weight<span class="token punctuation">:</span><span class="token operator">=</span>getattr<span class="token punctuation">(</span>son<span class="token punctuation">,</span> <span class="token string">"weight"</span><span class="token punctuation">,</span> None<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token operator">not</span> None<span class="token punctuation">:</span>
                self<span class="token punctuation">.</span>dtype<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>dtype <span class="token operator">=</span> weight<span class="token punctuation">.</span>dtype

    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> raw_x<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> torch<span class="token punctuation">.</span>Tensor<span class="token punctuation">:</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>input_trainsform<span class="token punctuation">(</span>raw_x<span class="token punctuation">)</span>
        y1 <span class="token operator">=</span> self<span class="token punctuation">.</span>residual_block1<span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token operator">+</span> x
        y2 <span class="token operator">=</span> self<span class="token punctuation">.</span>residual_block2<span class="token punctuation">(</span>y1<span class="token punctuation">)</span> <span class="token operator">+</span> y1 
        <span class="token comment" spellcheck="true"># y3 = self.residual_block3(y2) + y2</span>
        <span class="token comment" spellcheck="true"># y4 = self.residual_block4(y3) + y3</span>
        <span class="token keyword">return</span> self<span class="token punctuation">.</span>output<span class="token punctuation">(</span>y2<span class="token punctuation">)</span>

<span class="token keyword">class</span> <span class="token class-name">DeeperNormalizedNet</span><span class="token punctuation">(</span>_NetworkBase<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> in_features<span class="token punctuation">:</span> int<span class="token punctuation">)</span><span class="token punctuation">:</span>
        super<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>input_trainsform <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span>in_features<span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>residual_block1 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=64),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=128),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
        <span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>residual_block2 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=64),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true"># nn.BatchNorm1d(num_features=128),</span>
            <span class="token comment" spellcheck="true"># nn.Dropout(),</span>
        <span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>output <span class="token operator">=</span> nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>

        <span class="token keyword">for</span> name<span class="token punctuation">,</span> son <span class="token keyword">in</span> self<span class="token punctuation">.</span>input_trainsform<span class="token punctuation">.</span>named_children<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            weight<span class="token punctuation">:</span> nn<span class="token punctuation">.</span>parameter<span class="token punctuation">.</span>Parameter
            <span class="token keyword">if</span> <span class="token punctuation">(</span>weight<span class="token punctuation">:</span><span class="token operator">=</span>getattr<span class="token punctuation">(</span>son<span class="token punctuation">,</span> <span class="token string">"weight"</span><span class="token punctuation">,</span> None<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token operator">not</span> None<span class="token punctuation">:</span>
                self<span class="token punctuation">.</span>dtype<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>dtype <span class="token operator">=</span> weight<span class="token punctuation">.</span>dtype

    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> raw_x<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> torch<span class="token punctuation">.</span>Tensor<span class="token punctuation">:</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>input_trainsform<span class="token punctuation">(</span>raw_x<span class="token punctuation">)</span>
        y1 <span class="token operator">=</span> self<span class="token punctuation">.</span>residual_block1<span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token operator">+</span> x
        y2 <span class="token operator">=</span> self<span class="token punctuation">.</span>residual_block2<span class="token punctuation">(</span>y1<span class="token punctuation">)</span> <span class="token operator">+</span> y1
        <span class="token keyword">return</span> self<span class="token punctuation">.</span>output<span class="token punctuation">(</span>y2<span class="token punctuation">)</span>

<span class="token keyword">class</span> <span class="token class-name">WideNet</span><span class="token punctuation">(</span>_NetworkBase<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> in_features<span class="token punctuation">:</span> int<span class="token punctuation">)</span><span class="token punctuation">:</span>
        super<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>input_trainsform <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span>in_features<span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>residual_block1 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>residual_block2 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>residual_block3 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>output <span class="token operator">=</span> nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>

        <span class="token keyword">for</span> name<span class="token punctuation">,</span> son <span class="token keyword">in</span> self<span class="token punctuation">.</span>input_trainsform<span class="token punctuation">.</span>named_children<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            weight<span class="token punctuation">:</span> nn<span class="token punctuation">.</span>parameter<span class="token punctuation">.</span>Parameter
            <span class="token keyword">if</span> <span class="token punctuation">(</span>weight<span class="token punctuation">:</span><span class="token operator">=</span>getattr<span class="token punctuation">(</span>son<span class="token punctuation">,</span> <span class="token string">"weight"</span><span class="token punctuation">,</span> None<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token operator">not</span> None<span class="token punctuation">:</span>
                self<span class="token punctuation">.</span>dtype<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>dtype <span class="token operator">=</span> weight<span class="token punctuation">.</span>dtype

    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> raw_x<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> torch<span class="token punctuation">.</span>Tensor<span class="token punctuation">:</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>input_trainsform<span class="token punctuation">(</span>raw_x<span class="token punctuation">)</span>
        y11 <span class="token operator">=</span> self<span class="token punctuation">.</span>residual_block1<span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token operator">+</span> x
        y12 <span class="token operator">=</span> self<span class="token punctuation">.</span>residual_block2<span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token operator">+</span> x
        y1 <span class="token operator">=</span> <span class="token punctuation">(</span>y11 <span class="token operator">+</span> y12<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span>
        y2 <span class="token operator">=</span> self<span class="token punctuation">.</span>residual_block3<span class="token punctuation">(</span>y1<span class="token punctuation">)</span> <span class="token operator">+</span> y1
        <span class="token keyword">return</span> self<span class="token punctuation">.</span>output<span class="token punctuation">(</span>y2<span class="token punctuation">)</span>


<span class="token keyword">class</span> <span class="token class-name">OtherNet</span><span class="token punctuation">(</span>_NetworkBase<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> in_features<span class="token punctuation">:</span> int<span class="token punctuation">)</span><span class="token punctuation">:</span>
        super<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>net <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span>in_features<span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>BatchNorm1d<span class="token punctuation">(</span>num_features<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>Dropout<span class="token punctuation">(</span>p<span class="token operator">=</span><span class="token number">0.2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>LeakyReLU<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">,</span> out_features<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span>

        <span class="token keyword">for</span> name<span class="token punctuation">,</span> son <span class="token keyword">in</span> self<span class="token punctuation">.</span>net<span class="token punctuation">.</span>named_children<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            weight<span class="token punctuation">:</span> nn<span class="token punctuation">.</span>parameter<span class="token punctuation">.</span>Parameter
            <span class="token keyword">if</span> <span class="token punctuation">(</span>weight<span class="token punctuation">:</span><span class="token operator">=</span>getattr<span class="token punctuation">(</span>son<span class="token punctuation">,</span> <span class="token string">"weight"</span><span class="token punctuation">,</span> None<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token operator">not</span> None<span class="token punctuation">:</span>
                self<span class="token punctuation">.</span>dtype<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>dtype <span class="token operator">=</span> weight<span class="token punctuation">.</span>dtype


    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment" spellcheck="true"># x: [batch, channel]</span>
        <span class="token keyword">return</span> self<span class="token punctuation">.</span>net<span class="token punctuation">(</span>x<span class="token punctuation">)</span>



<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token punctuation">:</span>
    baseline <span class="token operator">=</span> Baseline<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">93</span><span class="token punctuation">)</span>
    x <span class="token operator">=</span> torch<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">64</span><span class="token punctuation">,</span> <span class="token number">93</span><span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">(</span>dtype<span class="token operator">=</span>baseline<span class="token punctuation">.</span>dtype<span class="token punctuation">,</span> device<span class="token operator">=</span>baseline<span class="token punctuation">.</span>device<span class="token punctuation">)</span>
    baseline<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="5-train-py"><a href="#5-train-py" class="headerlink" title="5. train.py"></a>5. train.py</h4><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">import</span> datetime
<span class="token keyword">import</span> typing
<span class="token keyword">from</span> pathlib <span class="token keyword">import</span> Path

<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd
<span class="token keyword">import</span> matplotlib <span class="token keyword">as</span> mpl
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">from</span> colorama <span class="token keyword">import</span> Fore<span class="token punctuation">,</span> Style
<span class="token keyword">from</span> matplotlib<span class="token punctuation">.</span>axes <span class="token keyword">import</span> Axes

<span class="token keyword">import</span> torch
<span class="token keyword">import</span> torch<span class="token punctuation">.</span>cuda
<span class="token keyword">import</span> torch<span class="token punctuation">.</span>backends<span class="token punctuation">.</span>cudnn
<span class="token keyword">import</span> torch<span class="token punctuation">.</span>optim <span class="token keyword">as</span> optim
<span class="token keyword">import</span> torch<span class="token punctuation">.</span>nn <span class="token keyword">as</span> nn
<span class="token keyword">from</span> torch<span class="token punctuation">.</span>optim <span class="token keyword">import</span> optimizer
<span class="token keyword">import</span> torch<span class="token punctuation">.</span>utils<span class="token punctuation">.</span>data <span class="token keyword">as</span> data


<span class="token keyword">import</span> networks
<span class="token keyword">from</span> pathconfig <span class="token keyword">import</span> <span class="token operator">*</span>
<span class="token keyword">from</span> dataset <span class="token keyword">import</span> <span class="token operator">*</span>



<span class="token keyword">class</span> <span class="token class-name">Trainer</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> network<span class="token punctuation">:</span> networks<span class="token punctuation">.</span>_NetworkBase<span class="token punctuation">,</span> dim2use<span class="token punctuation">:</span> typing<span class="token punctuation">.</span>List<span class="token punctuation">[</span>int<span class="token punctuation">]</span><span class="token operator">=</span>None<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> None<span class="token punctuation">:</span>
        super<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>start_time<span class="token punctuation">:</span> str <span class="token operator">=</span> datetime<span class="token punctuation">.</span>datetime<span class="token punctuation">.</span>now<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__str__<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>


        self<span class="token punctuation">.</span>select_device<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>reproducibile<span class="token punctuation">(</span><span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>network <span class="token operator">=</span> network<span class="token punctuation">.</span>to<span class="token punctuation">(</span>device<span class="token operator">=</span>self<span class="token punctuation">.</span>device<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>suffix<span class="token punctuation">:</span> str <span class="token operator">=</span> self<span class="token punctuation">.</span>network<span class="token punctuation">.</span>__class__<span class="token punctuation">.</span>__name__
        self<span class="token punctuation">.</span>log_folder<span class="token punctuation">:</span> Path <span class="token operator">=</span> Paths<span class="token punctuation">.</span>log_path <span class="token operator">/</span> self<span class="token punctuation">.</span>suffix <span class="token operator">/</span> self<span class="token punctuation">.</span>start_time<span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">":"</span><span class="token punctuation">,</span> <span class="token string">"_"</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>checkpoint_path<span class="token punctuation">:</span> Path <span class="token operator">=</span> Paths<span class="token punctuation">.</span>checkpoint_path <span class="token operator">/</span> self<span class="token punctuation">.</span>suffix <span class="token operator">/</span> f<span class="token string">"{self.start_time}.pt"</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">":"</span><span class="token punctuation">,</span> <span class="token string">"_"</span><span class="token punctuation">)</span>

        <span class="token keyword">if</span> <span class="token operator">not</span> self<span class="token punctuation">.</span>checkpoint_path<span class="token punctuation">.</span>parent<span class="token punctuation">.</span>exists<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            self<span class="token punctuation">.</span>checkpoint_path<span class="token punctuation">.</span>parent<span class="token punctuation">.</span>mkdir<span class="token punctuation">(</span>parents<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token operator">not</span> self<span class="token punctuation">.</span>log_folder<span class="token punctuation">.</span>exists<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            self<span class="token punctuation">.</span>log_folder<span class="token punctuation">.</span>mkdir<span class="token punctuation">(</span>parents<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>

        <span class="token comment" spellcheck="true"># train</span>
        self<span class="token punctuation">.</span>train_loader <span class="token operator">=</span> data<span class="token punctuation">.</span>DataLoader<span class="token punctuation">(</span>CovidDataset<span class="token punctuation">(</span>split<span class="token operator">=</span><span class="token string">"train"</span><span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">)</span><span class="token punctuation">,</span> batch_size<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> shuffle<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>val_loader <span class="token operator">=</span> data<span class="token punctuation">.</span>DataLoader<span class="token punctuation">(</span>CovidDataset<span class="token punctuation">(</span>split<span class="token operator">=</span><span class="token string">"val"</span><span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">)</span><span class="token punctuation">,</span> batch_size<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> shuffle<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true"># mse for regression</span>
        self<span class="token punctuation">.</span>lossfunc <span class="token operator">=</span> nn<span class="token punctuation">.</span>MSELoss<span class="token punctuation">(</span>reduction<span class="token operator">=</span><span class="token string">"mean"</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true"># self.lossfunc = nn.(reduction="mean")</span>

        <span class="token comment" spellcheck="true"># visualize</span>
        self<span class="token punctuation">.</span>train_loss<span class="token punctuation">:</span> typing<span class="token punctuation">.</span>List<span class="token punctuation">[</span>float<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
        self<span class="token punctuation">.</span>val_loss<span class="token punctuation">:</span> typing<span class="token punctuation">.</span>List<span class="token punctuation">[</span>float<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
        self<span class="token punctuation">.</span>min_val_loss<span class="token punctuation">:</span> float <span class="token operator">=</span> float<span class="token punctuation">(</span><span class="token string">"inf"</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">:</span> Axes
        _<span class="token punctuation">,</span> self<span class="token punctuation">.</span>axes <span class="token operator">=</span> plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span>nrows<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> ncols<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">select_device</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        self<span class="token punctuation">.</span>device <span class="token operator">=</span> <span class="token string">"cuda"</span> <span class="token keyword">if</span> torch<span class="token punctuation">.</span>cuda<span class="token punctuation">.</span>is_available<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">else</span> <span class="token string">"cpu"</span>

    <span class="token keyword">def</span> <span class="token function">reproducibile</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> seed<span class="token punctuation">:</span> int<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>seed<span class="token punctuation">(</span>seed<span class="token operator">=</span>seed<span class="token punctuation">)</span>
        torch<span class="token punctuation">.</span>manual_seed<span class="token punctuation">(</span>seed<span class="token operator">=</span>seed<span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true"># no optimization for convolution</span>
        torch<span class="token punctuation">.</span>backends<span class="token punctuation">.</span>cudnn<span class="token punctuation">.</span>benchmark <span class="token operator">=</span> <span class="token boolean">False</span>
        <span class="token comment" spellcheck="true"># use default convolution algorithm</span>
        torch<span class="token punctuation">.</span>backends<span class="token punctuation">.</span>cudnn<span class="token punctuation">.</span>deterministic <span class="token operator">=</span> <span class="token boolean">True</span>
        <span class="token keyword">if</span> self<span class="token punctuation">.</span>device <span class="token operator">==</span> <span class="token string">"cuda"</span><span class="token punctuation">:</span>
            torch<span class="token punctuation">.</span>cuda<span class="token punctuation">.</span>manual_seed<span class="token punctuation">(</span>seed<span class="token operator">=</span>seed<span class="token punctuation">)</span>
            torch<span class="token punctuation">.</span>cuda<span class="token punctuation">.</span>manual_seed<span class="token punctuation">(</span>seed<span class="token operator">=</span>seed<span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">visualize</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> val_loss<span class="token punctuation">:</span> float<span class="token punctuation">,</span> train_loss<span class="token punctuation">:</span> float<span class="token punctuation">)</span><span class="token punctuation">:</span>
        plt<span class="token punctuation">.</span>ion<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">.</span>cla<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">.</span>set_ylim<span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span> <span class="token number">400.0</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">.</span>set_xlabel<span class="token punctuation">(</span><span class="token string">"steps"</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">.</span>set_ylabel<span class="token punctuation">(</span><span class="token string">"loss / MSE"</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">.</span>set_title<span class="token punctuation">(</span>f<span class="token string">"{self.suffix}-{self.start_time}"</span><span class="token punctuation">)</span>


        x_train <span class="token operator">=</span> range<span class="token punctuation">(</span>len<span class="token punctuation">(</span>self<span class="token punctuation">.</span>train_loss<span class="token punctuation">)</span><span class="token punctuation">)</span>
        x_val <span class="token operator">=</span> x_train<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span>len<span class="token punctuation">(</span>self<span class="token punctuation">.</span>train_loss<span class="token punctuation">)</span> <span class="token operator">//</span> len<span class="token punctuation">(</span>self<span class="token punctuation">.</span>val_loss<span class="token punctuation">)</span><span class="token punctuation">]</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x_train<span class="token punctuation">,</span> self<span class="token punctuation">.</span>train_loss<span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token string">"tab:red"</span><span class="token punctuation">,</span> label<span class="token operator">=</span><span class="token string">"train loss"</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x_val<span class="token punctuation">,</span> self<span class="token punctuation">.</span>val_loss<span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token string">"tab:cyan"</span><span class="token punctuation">,</span> label<span class="token operator">=</span><span class="token string">"val loss"</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">.</span>plot<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> label<span class="token operator">=</span>f<span class="token string">"min_val_loss: {self.min_val_loss:>5.4f}"</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">.</span>plot<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> label<span class="token operator">=</span>f<span class="token string">"val/train loss: {val_loss:>5.4f}/{train_loss:>5.4f}"</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">.</span>text<span class="token punctuation">(</span>x<span class="token operator">=</span>len<span class="token punctuation">(</span>self<span class="token punctuation">.</span>train_loss<span class="token punctuation">)</span><span class="token operator">//</span><span class="token number">2</span><span class="token punctuation">,</span> y<span class="token operator">=</span><span class="token number">200</span><span class="token punctuation">,</span> s<span class="token operator">=</span>f<span class="token string">"Current loss: {self.train_loss[-1]:>5.4f}"</span><span class="token punctuation">,</span> fontsize<span class="token operator">=</span><span class="token number">24</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>axes<span class="token punctuation">.</span>legend<span class="token punctuation">(</span><span class="token punctuation">)</span>
        plt<span class="token punctuation">.</span>pause<span class="token punctuation">(</span><span class="token number">0.05</span><span class="token punctuation">)</span>
        plt<span class="token punctuation">.</span>ioff<span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">save</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> epoch<span class="token punctuation">:</span> int<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token string">"Saving model at {Fore.GREEN}{Style.BRIGHT}{epoch}{Style.RESET_ALL} epoch, val_loss = {Fore.GREEN}{Style.BRIGHT}{self.min_val_loss}{Style.RESET_ALL}"</span><span class="token punctuation">)</span>
        torch<span class="token punctuation">.</span>save<span class="token punctuation">(</span>self<span class="token punctuation">.</span>network<span class="token punctuation">.</span>state_dict<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> f<span class="token operator">=</span>self<span class="token punctuation">.</span>checkpoint_path<span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">lossfunc_l2</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> target<span class="token punctuation">,</span> pred<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> torch<span class="token punctuation">.</span>Tensor<span class="token punctuation">:</span>
        loss <span class="token operator">=</span> self<span class="token punctuation">.</span>lossfunc<span class="token punctuation">(</span>target<span class="token punctuation">,</span> pred<span class="token punctuation">)</span>
        norm_loss <span class="token operator">=</span> <span class="token number">0</span>
        <span class="token keyword">for</span> param <span class="token keyword">in</span> self<span class="token punctuation">.</span>network<span class="token punctuation">.</span>parameters<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            norm_loss <span class="token operator">+=</span> torch<span class="token punctuation">.</span>sum<span class="token punctuation">(</span>param<span class="token operator">**</span><span class="token number">2</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> loss <span class="token operator">+</span> norm_loss <span class="token operator">*</span> <span class="token number">0.00075</span>


    @torch<span class="token punctuation">.</span>no_grad<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">def</span> <span class="token function">validation</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        loss <span class="token operator">=</span> <span class="token number">0</span>
        x<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor
        y<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor
        self<span class="token punctuation">.</span>network<span class="token punctuation">.</span>eval<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">for</span> x<span class="token punctuation">,</span> y <span class="token keyword">in</span> self<span class="token punctuation">.</span>val_loader<span class="token punctuation">:</span>
            x<span class="token punctuation">,</span> y <span class="token operator">=</span> x<span class="token punctuation">.</span>to<span class="token punctuation">(</span>device<span class="token operator">=</span>self<span class="token punctuation">.</span>device<span class="token punctuation">,</span> dtype<span class="token operator">=</span>self<span class="token punctuation">.</span>network<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span><span class="token punctuation">,</span> y<span class="token punctuation">.</span>to<span class="token punctuation">(</span>device<span class="token operator">=</span>self<span class="token punctuation">.</span>device<span class="token punctuation">,</span> dtype<span class="token operator">=</span>self<span class="token punctuation">.</span>network<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span>
            y_pred<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor <span class="token operator">=</span> self<span class="token punctuation">.</span>network<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
            <span class="token comment" spellcheck="true"># loss += self.lossfunc(y, y_pred.squeeze()).sqrt()</span>
            <span class="token comment" spellcheck="true"># loss = self.lossfunc_l2(y, y_pred.squeeze())</span>
            loss <span class="token operator">+=</span> self<span class="token punctuation">.</span>lossfunc<span class="token punctuation">(</span>y<span class="token punctuation">,</span> y_pred<span class="token punctuation">.</span>squeeze<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        l<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor
        self<span class="token punctuation">.</span>val_loss<span class="token punctuation">.</span>append<span class="token punctuation">(</span>l<span class="token punctuation">:</span><span class="token operator">=</span><span class="token punctuation">(</span>loss<span class="token operator">/</span>len<span class="token punctuation">(</span>self<span class="token punctuation">.</span>val_loader<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>cpu<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>item<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> l

    <span class="token keyword">def</span> <span class="token function">train</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> lr<span class="token punctuation">:</span> float<span class="token operator">=</span><span class="token number">1e</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">,</span> wd<span class="token punctuation">:</span> float<span class="token operator">=</span><span class="token number">0.9</span><span class="token punctuation">,</span> n_epoch<span class="token punctuation">:</span> int<span class="token operator">=</span><span class="token number">3000</span><span class="token punctuation">,</span> momentum<span class="token operator">=</span><span class="token number">0.9</span><span class="token punctuation">,</span> early_stop<span class="token punctuation">:</span> int<span class="token operator">=</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        msg<span class="token punctuation">:</span> str <span class="token operator">=</span> input<span class="token punctuation">(</span><span class="token string">"请输入本次训练的日志记录："</span><span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true"># optimizer = optim.SGD(self.network.parameters(), lr=lr, weight_decay=wd, momentum=momentum)</span>
        optimizer <span class="token operator">=</span> optim<span class="token punctuation">.</span>AdamW<span class="token punctuation">(</span>self<span class="token punctuation">.</span>network<span class="token punctuation">.</span>parameters<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> lr<span class="token operator">=</span>lr<span class="token punctuation">,</span> weight_decay<span class="token operator">=</span>wd<span class="token punctuation">)</span>
        <span class="token keyword">with</span> open<span class="token punctuation">(</span>self<span class="token punctuation">.</span>log_folder<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"message.txt"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"w"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
            f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>msg<span class="token operator">+</span><span class="token string">"\n"</span><span class="token punctuation">)</span>
            f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>f<span class="token string">"epoch: {n_epoch}, lr: {lr}, wd: {wd}, early_stop: {early_stop}, optimizer: {optimizer.__class__.__name__}\n"</span><span class="token punctuation">)</span>
        early_stop_cnt <span class="token operator">=</span> <span class="token number">0</span>
        <span class="token keyword">for</span> epoch <span class="token keyword">in</span> range<span class="token punctuation">(</span>n_epoch<span class="token punctuation">)</span><span class="token punctuation">:</span>
            x<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor
            y<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor
            self<span class="token punctuation">.</span>network<span class="token punctuation">.</span>train<span class="token punctuation">(</span><span class="token punctuation">)</span>
            train_loss<span class="token punctuation">:</span> float <span class="token operator">=</span> <span class="token number">0</span>
            <span class="token keyword">for</span> x<span class="token punctuation">,</span> y <span class="token keyword">in</span> self<span class="token punctuation">.</span>train_loader<span class="token punctuation">:</span>
                optimizer<span class="token punctuation">.</span>zero_grad<span class="token punctuation">(</span><span class="token punctuation">)</span>
                x<span class="token punctuation">,</span> y <span class="token operator">=</span> x<span class="token punctuation">.</span>to<span class="token punctuation">(</span>device<span class="token operator">=</span>self<span class="token punctuation">.</span>device<span class="token punctuation">,</span> dtype<span class="token operator">=</span>self<span class="token punctuation">.</span>network<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span><span class="token punctuation">,</span> y<span class="token punctuation">.</span>to<span class="token punctuation">(</span>device<span class="token operator">=</span>self<span class="token punctuation">.</span>device<span class="token punctuation">,</span> dtype<span class="token operator">=</span>self<span class="token punctuation">.</span>network<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span>
                y_pred<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor <span class="token operator">=</span> self<span class="token punctuation">.</span>network<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
                loss<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor <span class="token operator">=</span> self<span class="token punctuation">.</span>lossfunc<span class="token punctuation">(</span>y<span class="token punctuation">,</span> y_pred<span class="token punctuation">.</span>squeeze<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                <span class="token comment" spellcheck="true"># loss = self.lossfunc_l2(y, y_pred.squeeze())</span>
                <span class="token comment" spellcheck="true"># loss = loss.sqrt()</span>
                loss<span class="token punctuation">.</span>backward<span class="token punctuation">(</span><span class="token punctuation">)</span>
                optimizer<span class="token punctuation">.</span>step<span class="token punctuation">(</span><span class="token punctuation">)</span>
                train_loss <span class="token operator">+=</span> loss<span class="token punctuation">.</span>clone<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>detach<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>cpu<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>squeeze<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>item<span class="token punctuation">(</span><span class="token punctuation">)</span>
                self<span class="token punctuation">.</span>train_loss<span class="token punctuation">.</span>append<span class="token punctuation">(</span>loss<span class="token punctuation">.</span>clone<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>detach<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>cpu<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>squeeze<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>item<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

            train_loss <span class="token operator">=</span> <span class="token punctuation">(</span>train_loss <span class="token operator">/</span> len<span class="token punctuation">(</span>self<span class="token punctuation">.</span>train_loader<span class="token punctuation">)</span><span class="token punctuation">)</span>
            val_loss <span class="token operator">=</span> self<span class="token punctuation">.</span>validation<span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword">if</span> val_loss <span class="token operator">&lt;</span> self<span class="token punctuation">.</span>min_val_loss<span class="token punctuation">:</span>
                self<span class="token punctuation">.</span>min_val_loss <span class="token operator">=</span> val_loss
                early_stop_cnt <span class="token operator">=</span> <span class="token number">0</span>
                self<span class="token punctuation">.</span>save<span class="token punctuation">(</span>epoch<span class="token operator">=</span>epoch<span class="token punctuation">)</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                early_stop_cnt <span class="token operator">+=</span> <span class="token number">1</span>
            self<span class="token punctuation">.</span>visualize<span class="token punctuation">(</span>val_loss<span class="token operator">=</span>val_loss<span class="token punctuation">,</span> train_loss<span class="token operator">=</span>train_loss<span class="token punctuation">)</span>
            <span class="token keyword">if</span> early_stop_cnt <span class="token operator">>=</span> early_stop<span class="token punctuation">:</span>
                <span class="token keyword">break</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>green<span class="token punctuation">(</span>f<span class="token string">"Training stopped at {epoch}"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>green<span class="token punctuation">(</span><span class="token string">"Saving trainig log"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">with</span> open<span class="token punctuation">(</span>self<span class="token punctuation">.</span>log_folder<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"train_val.loss.pkl"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"wb"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
            pickle<span class="token punctuation">.</span>dump<span class="token punctuation">(</span>
                <span class="token punctuation">{</span>
                    <span class="token string">"val"</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>val_loss<span class="token punctuation">,</span>
                    <span class="token string">"train"</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>train_loss<span class="token punctuation">,</span>
                    <span class="token string">"min_val"</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>min_val_loss
                <span class="token punctuation">}</span><span class="token punctuation">,</span> file<span class="token operator">=</span>f
            <span class="token punctuation">)</span>
        plt<span class="token punctuation">.</span>savefig<span class="token punctuation">(</span>self<span class="token punctuation">.</span>log_folder<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"train_log.png"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__str__<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> dpi<span class="token operator">=</span><span class="token number">300</span><span class="token punctuation">)</span>

    <span class="token comment" spellcheck="true"># @torch.no_grad()</span>
    <span class="token comment" spellcheck="true"># def get_presudo</span>



<span class="token keyword">def</span> <span class="token function">baseline</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    b <span class="token operator">=</span> networks<span class="token punctuation">.</span>Baseline<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">93</span><span class="token punctuation">)</span>
    t <span class="token operator">=</span> Trainer<span class="token punctuation">(</span>network<span class="token operator">=</span>b<span class="token punctuation">)</span>
    t<span class="token punctuation">.</span>train<span class="token punctuation">(</span>wd<span class="token operator">=</span><span class="token number">0.5</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">previous2day</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    dim2use <span class="token operator">=</span> list<span class="token punctuation">(</span>range<span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    dim2use<span class="token punctuation">.</span>extend<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">57</span><span class="token punctuation">,</span> <span class="token number">75</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    b <span class="token operator">=</span> networks<span class="token punctuation">.</span>Baseline<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">42</span><span class="token punctuation">)</span>
    t <span class="token operator">=</span> Trainer<span class="token punctuation">(</span>network<span class="token operator">=</span>b<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">)</span>
    t<span class="token punctuation">.</span>train<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">selected_feature</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    dim2use <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">76</span><span class="token punctuation">,</span> <span class="token number">58</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
    b <span class="token operator">=</span> networks<span class="token punctuation">.</span>Baseline<span class="token punctuation">(</span>in_features<span class="token operator">=</span>len<span class="token punctuation">(</span>dim2use<span class="token punctuation">)</span><span class="token punctuation">)</span>
    t <span class="token operator">=</span> Trainer<span class="token punctuation">(</span>network<span class="token operator">=</span>b<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">)</span>
    t<span class="token punctuation">.</span>train<span class="token punctuation">(</span><span class="token punctuation">)</span>


<span class="token keyword">def</span> <span class="token function">residual</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># dim2use = np.array([76, 58]) - 1</span>
    dim2use <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">75</span><span class="token punctuation">,</span> <span class="token number">57</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token number">78</span><span class="token punctuation">,</span> <span class="token number">43</span><span class="token punctuation">,</span> <span class="token number">61</span><span class="token punctuation">,</span> <span class="token number">79</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">58</span><span class="token punctuation">,</span> <span class="token number">76</span><span class="token punctuation">,</span> <span class="token number">41</span><span class="token punctuation">,</span> <span class="token number">59</span><span class="token punctuation">,</span> <span class="token number">77</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># dim2use = np.array([76, 58, 43, 61, 79, 44, 62, 80, 41, 59, 77, 42, 60, 78, 93, 75, 57, 88, 70, 52, 84, 66, 48]) - 1</span>
    <span class="token comment" spellcheck="true"># dim2use = np.array([75, 57, 61, 79, 43, 78, 60, 42, 91, 73, 83, 80, 68, 62, 40, 86, 65, 77, 85, 67, 55, 49])</span>
    d <span class="token operator">=</span> networks<span class="token punctuation">.</span>DeeperNet<span class="token punctuation">(</span>in_features<span class="token operator">=</span>len<span class="token punctuation">(</span>dim2use<span class="token punctuation">)</span><span class="token punctuation">)</span>
    t <span class="token operator">=</span> Trainer<span class="token punctuation">(</span>network<span class="token operator">=</span>d<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">)</span>
    t<span class="token punctuation">.</span>train<span class="token punctuation">(</span>wd<span class="token operator">=</span><span class="token number">0.5</span><span class="token punctuation">,</span> lr<span class="token operator">=</span><span class="token number">1e</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">normalized</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    dim2use <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">76</span><span class="token punctuation">,</span> <span class="token number">58</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
    d <span class="token operator">=</span> networks<span class="token punctuation">.</span>DeeperNormalizedNet<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
    t <span class="token operator">=</span> Trainer<span class="token punctuation">(</span>network<span class="token operator">=</span>d<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">)</span>
    t<span class="token punctuation">.</span>train<span class="token punctuation">(</span>wd<span class="token operator">=</span><span class="token number">0.5</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">wider</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    dim2use <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">76</span><span class="token punctuation">,</span> <span class="token number">58</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
    w <span class="token operator">=</span> networks<span class="token punctuation">.</span>WideNet<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
    t <span class="token operator">=</span> Trainer<span class="token punctuation">(</span>network<span class="token operator">=</span>w<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">)</span>
    t<span class="token punctuation">.</span>train<span class="token punctuation">(</span>wd<span class="token operator">=</span><span class="token number">0.5</span><span class="token punctuation">,</span> lr<span class="token operator">=</span><span class="token number">1e</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">)</span>


<span class="token keyword">def</span> <span class="token function">other</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    dim2use <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">75</span><span class="token punctuation">,</span> <span class="token number">57</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token number">78</span><span class="token punctuation">,</span> <span class="token number">43</span><span class="token punctuation">,</span> <span class="token number">61</span><span class="token punctuation">,</span> <span class="token number">79</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">58</span><span class="token punctuation">,</span> <span class="token number">76</span><span class="token punctuation">,</span> <span class="token number">41</span><span class="token punctuation">,</span> <span class="token number">59</span><span class="token punctuation">,</span> <span class="token number">77</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    o <span class="token operator">=</span> networks<span class="token punctuation">.</span>OtherNet<span class="token punctuation">(</span>in_features<span class="token operator">=</span>len<span class="token punctuation">(</span>dim2use<span class="token punctuation">)</span><span class="token punctuation">)</span>
    t <span class="token operator">=</span> Trainer<span class="token punctuation">(</span>network<span class="token operator">=</span>o<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">)</span>
    t<span class="token punctuation">.</span>train<span class="token punctuation">(</span>wd<span class="token operator">=</span><span class="token number">0.5</span><span class="token punctuation">,</span> lr<span class="token operator">=</span><span class="token number">1e</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">)</span>



<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># baseline()</span>
    <span class="token comment" spellcheck="true"># previous2day()</span>
    <span class="token comment" spellcheck="true"># selected_feature()</span>
    residual<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># other()</span>
    <span class="token comment" spellcheck="true"># normalized()</span>
    <span class="token comment" spellcheck="true"># wider()</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="6-gen-submission-py"><a href="#6-gen-submission-py" class="headerlink" title="6. gen_submission.py"></a>6. gen_submission.py</h4><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">import</span> typing

<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd

<span class="token keyword">import</span> torch
<span class="token keyword">import</span> torch<span class="token punctuation">.</span>utils<span class="token punctuation">.</span>data <span class="token keyword">as</span> data
<span class="token keyword">from</span> dataset <span class="token keyword">import</span> CovidDataset


<span class="token keyword">import</span> networks
<span class="token keyword">from</span> pathconfig <span class="token keyword">import</span> <span class="token operator">*</span>




@torch<span class="token punctuation">.</span>no_grad<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">gen_submission</span><span class="token punctuation">(</span>network<span class="token punctuation">:</span>networks<span class="token punctuation">.</span>_NetworkBase<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>None<span class="token punctuation">,</span> save_path<span class="token punctuation">:</span> Path<span class="token operator">=</span>None<span class="token punctuation">)</span><span class="token punctuation">:</span>
    test_loader <span class="token operator">=</span> data<span class="token punctuation">.</span>DataLoader<span class="token punctuation">(</span>dataset<span class="token operator">=</span>CovidDataset<span class="token punctuation">(</span>split<span class="token operator">=</span><span class="token string">"test"</span><span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">)</span><span class="token punctuation">,</span> batch_size<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">)</span>
    result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    x<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor
    <span class="token keyword">for</span> x <span class="token keyword">in</span> test_loader<span class="token punctuation">:</span>
        x <span class="token operator">=</span> x<span class="token punctuation">.</span>to<span class="token punctuation">(</span>device<span class="token operator">=</span><span class="token string">"cuda"</span><span class="token punctuation">,</span> dtype<span class="token operator">=</span>network<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span>
        y_pred<span class="token punctuation">:</span> torch<span class="token punctuation">.</span>Tensor <span class="token operator">=</span> network<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        result<span class="token punctuation">.</span>extend<span class="token punctuation">(</span>y_pred<span class="token punctuation">.</span>cpu<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>squeeze<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>tolist<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token operator">not</span> save_path<span class="token punctuation">.</span>parent<span class="token punctuation">.</span>exists<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        save_path<span class="token punctuation">.</span>parent<span class="token punctuation">.</span>mkdir<span class="token punctuation">(</span>parents<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
    result <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>result<span class="token punctuation">)</span>
    <span class="token keyword">with</span> open<span class="token punctuation">(</span>save_path<span class="token punctuation">,</span> <span class="token string">"w"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>save_path<span class="token punctuation">)</span>
        f<span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token string">"id,tested_positive\n"</span><span class="token punctuation">)</span>
        <span class="token keyword">for</span> id<span class="token punctuation">,</span> r <span class="token keyword">in</span> enumerate<span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">:</span>
            f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>f<span class="token string">"{id},{r}\n"</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">gen_baseline</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    net <span class="token operator">=</span> networks<span class="token punctuation">.</span>Baseline<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">93</span><span class="token punctuation">)</span><span class="token punctuation">.</span>cuda<span class="token punctuation">(</span><span class="token punctuation">)</span>
    net<span class="token punctuation">.</span>load_state_dict<span class="token punctuation">(</span>torch<span class="token punctuation">.</span>load<span class="token punctuation">(</span>s<span class="token punctuation">:</span><span class="token operator">=</span>Paths<span class="token punctuation">.</span>checkpoint_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"../checkpoint/Baseline/2021-12-12 15_29_31.pt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    gen_submission<span class="token punctuation">(</span>network<span class="token operator">=</span>net<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>None<span class="token punctuation">,</span> save_path<span class="token operator">=</span>Paths<span class="token punctuation">.</span>submission_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span>net<span class="token punctuation">.</span>__class__<span class="token punctuation">.</span>__name__<span class="token punctuation">,</span> s<span class="token punctuation">.</span>stem<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">".csv"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">gen2day</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    net <span class="token operator">=</span> networks<span class="token punctuation">.</span>Baseline<span class="token punctuation">(</span>in_features<span class="token operator">=</span><span class="token number">42</span><span class="token punctuation">)</span><span class="token punctuation">.</span>cuda<span class="token punctuation">(</span><span class="token punctuation">)</span>
    net<span class="token punctuation">.</span>load_state_dict<span class="token punctuation">(</span>torch<span class="token punctuation">.</span>load<span class="token punctuation">(</span>s<span class="token punctuation">:</span><span class="token operator">=</span>Paths<span class="token punctuation">.</span>checkpoint_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"../checkpoint/Baseline/2021-12-12 15_40_58.pt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    dim2use <span class="token operator">=</span> list<span class="token punctuation">(</span>range<span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    dim2use<span class="token punctuation">.</span>extend<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">57</span><span class="token punctuation">,</span> <span class="token number">75</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    gen_submission<span class="token punctuation">(</span>network<span class="token operator">=</span>net<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">,</span> save_path<span class="token operator">=</span>Paths<span class="token punctuation">.</span>submission_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span>net<span class="token punctuation">.</span>__class__<span class="token punctuation">.</span>__name__<span class="token punctuation">,</span> s<span class="token punctuation">.</span>stem<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">".csv"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>


<span class="token keyword">def</span> <span class="token function">feature_selection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># dim2use = np.array([76, 58, 43, 61, 79, 44, 62, 80, 41, 59, 77, 42, 60, 78, 93]) - 1</span>
    dim2use <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">76</span><span class="token punctuation">,</span> <span class="token number">58</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
    net <span class="token operator">=</span> networks<span class="token punctuation">.</span>Baseline<span class="token punctuation">(</span>in_features<span class="token operator">=</span>len<span class="token punctuation">(</span>dim2use<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>cuda<span class="token punctuation">(</span><span class="token punctuation">)</span>
    net<span class="token punctuation">.</span>load_state_dict<span class="token punctuation">(</span>torch<span class="token punctuation">.</span>load<span class="token punctuation">(</span>s<span class="token punctuation">:</span><span class="token operator">=</span>Paths<span class="token punctuation">.</span>checkpoint_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"../checkpoint/Baseline/2021-12-12 16_33_04.pt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    gen_submission<span class="token punctuation">(</span>network<span class="token operator">=</span>net<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">,</span> save_path<span class="token operator">=</span>Paths<span class="token punctuation">.</span>submission_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span>net<span class="token punctuation">.</span>__class__<span class="token punctuation">.</span>__name__<span class="token punctuation">,</span> s<span class="token punctuation">.</span>stem<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">".csv"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>


<span class="token keyword">def</span> <span class="token function">residual</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># dim2use = np.array([76, 58, 43, 61, 79, 44, 62, 80, 41, 59, 77, 42, 60, 78, 93]) - 1</span>
    <span class="token comment" spellcheck="true"># dim2use = np.array([76, 58]) - 1</span>
    dim2use <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">75</span><span class="token punctuation">,</span> <span class="token number">57</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token number">78</span><span class="token punctuation">,</span> <span class="token number">43</span><span class="token punctuation">,</span> <span class="token number">61</span><span class="token punctuation">,</span> <span class="token number">79</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">58</span><span class="token punctuation">,</span> <span class="token number">76</span><span class="token punctuation">,</span> <span class="token number">41</span><span class="token punctuation">,</span> <span class="token number">59</span><span class="token punctuation">,</span> <span class="token number">77</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># dim2use = np.array([75, 57, 61, 79, 43, 78, 60, 42, 91, 73, 83, 80, 68, 62, 40, 86, 65, 77, 85, 67, 55, 49])</span>
    <span class="token comment" spellcheck="true"># dim2use = np.array([76, 58, 43, 61, 79, 44, 62, 80, 41, 59, 77, 42, 60, 78, 93, 75, 57, 88, 70, 52, 84, 66, 48]) - 1</span>
    <span class="token comment" spellcheck="true"># dim2use = np.array([75, 57])</span>
    net <span class="token operator">=</span> networks<span class="token punctuation">.</span>DeeperNet<span class="token punctuation">(</span>in_features<span class="token operator">=</span>len<span class="token punctuation">(</span>dim2use<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>cuda<span class="token punctuation">(</span><span class="token punctuation">)</span>
    net<span class="token punctuation">.</span>load_state_dict<span class="token punctuation">(</span>torch<span class="token punctuation">.</span>load<span class="token punctuation">(</span>s<span class="token punctuation">:</span><span class="token operator">=</span>Paths<span class="token punctuation">.</span>checkpoint_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"../checkpoint/DeeperNet/2021-12-13 01_55_10.pt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    gen_submission<span class="token punctuation">(</span>network<span class="token operator">=</span>net<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">,</span> save_path<span class="token operator">=</span>Paths<span class="token punctuation">.</span>submission_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span>net<span class="token punctuation">.</span>__class__<span class="token punctuation">.</span>__name__<span class="token punctuation">,</span> s<span class="token punctuation">.</span>stem<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">".csv"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">normalize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># dim2use = np.array([76, 58, 43, 61, 79, 44, 62, 80, 41, 59, 77, 42, 60, 78, 93]) - 1</span>
    dim2use <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">76</span><span class="token punctuation">,</span> <span class="token number">58</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
    net <span class="token operator">=</span> networks<span class="token punctuation">.</span>DeeperNormalizedNet<span class="token punctuation">(</span>in_features<span class="token operator">=</span>len<span class="token punctuation">(</span>dim2use<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>cuda<span class="token punctuation">(</span><span class="token punctuation">)</span>
    net<span class="token punctuation">.</span>load_state_dict<span class="token punctuation">(</span>torch<span class="token punctuation">.</span>load<span class="token punctuation">(</span>s<span class="token punctuation">:</span><span class="token operator">=</span>Paths<span class="token punctuation">.</span>checkpoint_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"../checkpoint/DeeperNormalizedNet/2021-12-12 17_55_08.pt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    gen_submission<span class="token punctuation">(</span>network<span class="token operator">=</span>net<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">,</span> save_path<span class="token operator">=</span>Paths<span class="token punctuation">.</span>submission_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span>net<span class="token punctuation">.</span>__class__<span class="token punctuation">.</span>__name__<span class="token punctuation">,</span> s<span class="token punctuation">.</span>stem<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">".csv"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">wider</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    dim2use <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">76</span><span class="token punctuation">,</span> <span class="token number">58</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span>
    net <span class="token operator">=</span> networks<span class="token punctuation">.</span>WideNet<span class="token punctuation">(</span>in_features<span class="token operator">=</span>len<span class="token punctuation">(</span>dim2use<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>cuda<span class="token punctuation">(</span><span class="token punctuation">)</span>
    net<span class="token punctuation">.</span>load_state_dict<span class="token punctuation">(</span>torch<span class="token punctuation">.</span>load<span class="token punctuation">(</span>s<span class="token punctuation">:</span><span class="token operator">=</span>Paths<span class="token punctuation">.</span>checkpoint_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"../checkpoint/WideNet/2021-12-12 18_12_38.pt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    gen_submission<span class="token punctuation">(</span>network<span class="token operator">=</span>net<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">,</span> save_path<span class="token operator">=</span>Paths<span class="token punctuation">.</span>submission_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span>net<span class="token punctuation">.</span>__class__<span class="token punctuation">.</span>__name__<span class="token punctuation">,</span> s<span class="token punctuation">.</span>stem<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">".csv"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">other</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    dim2use <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">75</span><span class="token punctuation">,</span> <span class="token number">57</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token number">78</span><span class="token punctuation">,</span> <span class="token number">43</span><span class="token punctuation">,</span> <span class="token number">61</span><span class="token punctuation">,</span> <span class="token number">79</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">58</span><span class="token punctuation">,</span> <span class="token number">76</span><span class="token punctuation">,</span> <span class="token number">41</span><span class="token punctuation">,</span> <span class="token number">59</span><span class="token punctuation">,</span> <span class="token number">77</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    net <span class="token operator">=</span> networks<span class="token punctuation">.</span>OtherNet<span class="token punctuation">(</span>in_features<span class="token operator">=</span>len<span class="token punctuation">(</span>dim2use<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>cuda<span class="token punctuation">(</span><span class="token punctuation">)</span>
    net<span class="token punctuation">.</span>load_state_dict<span class="token punctuation">(</span>torch<span class="token punctuation">.</span>load<span class="token punctuation">(</span>s<span class="token punctuation">:</span><span class="token operator">=</span>Paths<span class="token punctuation">.</span>checkpoint_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span><span class="token string">"../checkpoint/OtherNet/2021-12-13 00_49_48.pt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    gen_submission<span class="token punctuation">(</span>network<span class="token operator">=</span>net<span class="token punctuation">,</span> dim2use<span class="token operator">=</span>dim2use<span class="token punctuation">,</span> save_path<span class="token operator">=</span>Paths<span class="token punctuation">.</span>submission_path<span class="token punctuation">.</span>joinpath<span class="token punctuation">(</span>net<span class="token punctuation">.</span>__class__<span class="token punctuation">.</span>__name__<span class="token punctuation">,</span> s<span class="token punctuation">.</span>stem<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">".csv"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># gen_baseline()</span>
    <span class="token comment" spellcheck="true"># gen2day()</span>
    <span class="token comment" spellcheck="true"># feature_selection()</span>
    <span class="token comment" spellcheck="true"># other()</span>
    residual<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># normalize()</span>
    <span class="token comment" spellcheck="true"># wider()</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Jack Wang</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://jackwang0107.github.io/2021/12/11/li-hong-yi-ml2021-spring-hw1-covid19-cases-prediction-md/">https://jackwang0107.github.io/2021/12/11/li-hong-yi-ml2021-spring-hw1-covid19-cases-prediction-md/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Jack Wang</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/Deep-Learning/">
                                    <span class="chip bg-color">Deep Learning</span>
                                </a>
                            
                                <a href="/tags/Hungyi-Li/">
                                    <span class="chip bg-color">Hungyi Li</span>
                                </a>
                            
                                <a href="/tags/Machine-Learning/">
                                    <span class="chip bg-color">Machine Learning</span>
                                </a>
                            
                                <a href="/tags/Neural-Network/">
                                    <span class="chip bg-color">Neural Network</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2021/12/16/pyqt5-xue-xi-bi-ji-0-chu-xin/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211216205416019.png" class="responsive-img" alt="PyQt5学习笔记 Chapter 0-Introduction： Part 0.初心">
                        
                        <span class="card-title">PyQt5学习笔记 Chapter 0-Introduction： Part 0.初心</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本文作为PyQt系列学习笔记的第一篇，记录了我为什么要学习PyQt，以及本系列文章的组织形式
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-12-16
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/PyQt5-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="post-category">
                                    PyQt5 学习笔记
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Python/">
                        <span class="chip bg-color">Python</span>
                    </a>
                    
                    <a href="/tags/GUI%E5%BC%80%E5%8F%91/">
                        <span class="chip bg-color">GUI开发</span>
                    </a>
                    
                    <a href="/tags/PyQt/">
                        <span class="chip bg-color">PyQt</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2021/12/11/li-hong-yi-ml2021-spring-hw0-gai-shu/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211207145714311.png" class="responsive-img" alt="李宏毅ML2021 Spring homeworks: hw0 -- 概述">
                        
                        <span class="card-title">李宏毅ML2021 Spring homeworks: hw0 -- 概述</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本文讲解李宏毅ML2021 Spring homeworks系列文章的写作原因以及系列文章的写作目的
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-12-11
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E6%9D%8E%E5%AE%8F%E6%AF%85ML2021-Spring-Homeworks/" class="post-category">
                                    李宏毅ML2021 Spring Homeworks
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Deep-Learning/">
                        <span class="chip bg-color">Deep Learning</span>
                    </a>
                    
                    <a href="/tags/Hungyi-Li/">
                        <span class="chip bg-color">Hungyi Li</span>
                    </a>
                    
                    <a href="/tags/Machine-Learning/">
                        <span class="chip bg-color">Machine Learning</span>
                    </a>
                    
                    <a href="/tags/CNN/">
                        <span class="chip bg-color">CNN</span>
                    </a>
                    
                    <a href="/tags/Attention/">
                        <span class="chip bg-color">Attention</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE' || selection.getRangeAt(0).commonAncestorContainer.nodeName === 'CODE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: JackWang&#39;s Blog<br />'
            + '文章作者: Jack Wang<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('2'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2023</span>
            
            <a href="/about" target="_blank">Jack Wang</a>
            <!-- |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a> -->
            <!-- |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a> -->
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">603.8k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
                <span id="sitetime"> Loading ...</span>
                <script>
                    var calcSiteTime = function () {
                        var seconds = 1000;
                        var minutes = seconds * 60;
                        var hours = minutes * 60;
                        var days = hours * 24;
                        var years = days * 365;
                        var today = new Date();
                        var startYear = "2021";
                        var startMonth = "11";
                        var startDate = "12";
                        var startHour = "0";
                        var startMinute = "0";
                        var startSecond = "0";
                        var todayYear = today.getFullYear();
                        var todayMonth = today.getMonth() + 1;
                        var todayDate = today.getDate();
                        var todayHour = today.getHours();
                        var todayMinute = today.getMinutes();
                        var todaySecond = today.getSeconds();
                        var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                        var diff = t2 - t1;
                        var diffYears = Math.floor(diff / years);
                        var diffDays = Math.floor((diff / days) - diffYears * 365);

                        // 区分是否有年份.
                        var language = 'zh-CN';
                        if (startYear === String(todayYear)) {
                            document.getElementById("year").innerHTML = todayYear;
                            var daysTip = 'This site has been running for ' + diffDays + ' days';
                            if (language === 'zh-CN') {
                                daysTip = '本站已运行 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                daysTip = '本站已運行 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = daysTip;
                        } else {
                            document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                            var yearsAndDaysTip = 'This site has been running for ' + diffYears + ' years and '
                                + diffDays + ' days';
                            if (language === 'zh-CN') {
                                yearsAndDaysTip = '本站已运行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                yearsAndDaysTip = '本站已運行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = yearsAndDaysTip;
                        }
                    }

                    calcSiteTime();
                </script>
            
            <br>
            
                <span id="icp"><img src="/medias/icp.png"
                                    style="vertical-align: text-bottom;"/>
                <a href="https://beian.miit.gov.cn" target="_blank">陕ICP备2021014294号-1</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/jackwang0108" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2232123545@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2232123545" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2232123545" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    
        
        <script type="text/javascript">
            // 只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/libs/others/sakura.js"><\/script>');
            }
        </script>
    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
